import { clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs) {
  return twMerge(clsx(inputs))
}


// 判断是不是pc端
export function isPC() {
  var ispc=true
  var uA = navigator.userAgent.toLowerCase();
  var ipad = uA.match(/ipad/i) == "ipad";
  var iphone = uA.match(/iphone os/i) == "iphone os";
  var midp = uA.match(/midp/i) == "midp";
  var uc7 = uA.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var uc = uA.match(/ucweb/i) == "ucweb";
  var android = uA.match(/android/i) == "android";
  var windowsce = uA.match(/windows ce/i) == "windows ce";
  var windowsmd = uA.match(/windows mobile/i) == "windows mobile"; 
  if (!(ipad || iphone || midp || uc7 || uc || android || windowsce || windowsmd)) {
      // PC 端
      ispc=true
  }else{
      ispc=false
      // 移动端
  }
  return ispc
}


// 引入 codemirror
export function loadCodemirror() {
  return new Promise((resolve, reject) => {
      // 远程加载js （按需引入）

    if(window.loadCodemirror){
      resolve();
      return;
    }

    var urls = [
      // css
      '/lib/codemirror-5.31.0/lib/codemirror.css',
      '/lib/codemirror-5.31.0/addon/fold/foldgutter.css',
      '/lib/codemirror-5.31.0/addon/hint/show-hint.css',
      '/lib/codemirror-5.31.0/addon/lint/lint.css',
      '/lib/codemirror-5.31.0/addon/merge/merge.css',
      '/lib/codemirror-5.31.0/addon/display/fullscreen.css',

      '/lib/codemirror-5.31.0/theme/monokai.css',
      '/lib/codemirror-5.31.0/theme/eclipse.css',
      '/lib/codemirror-5.31.0/theme/midnight.css',
      '/lib/codemirror-5.31.0/theme/material.css',
      '/lib/codemirror-5.31.0/theme/seti.css',

      // js
      '/lib/codemirror-5.31.0/lib/codemirror.js',
      '/lib/codemirror-5.31.0/addon/comment/comment.js',
      '/lib/codemirror-5.31.0/addon/selection/active-line.js',
      '/lib/codemirror-5.31.0/keymap/sublime.js',
      '/lib/codemirror-5.31.0/addon/hint/show-hint.js',
      '/lib/codemirror-5.31.0/addon/hint/sql-hint.js',
      '/lib/codemirror-5.31.0/mode/python/python.js',
      '/lib/codemirror-5.31.0/mode/sql/sql.js',

      '/lib/codemirror-5.31.0/mode/htmlmixed/htmlmixed.js',
      '/lib/codemirror-5.31.0/mode/xml/xml.js',
      '/lib/codemirror-5.31.0/mode/javascript/javascript.js',
      '/lib/codemirror-5.31.0/mode/css/css.js',

      '/lib/codemirror-5.31.0/addon/hint/html-hint.js',
      '/lib/codemirror-5.31.0/addon/lint/lint.js',
      '/lib/codemirror-5.31.0/addon/lint/javascript-lint.js',
      '/lib/codemirror-5.31.0/addon/lint/html-lint.js',


      '/lib/codemirror-5.31.0/addon/fold/foldcode.js',
      '/lib/codemirror-5.31.0/addon/fold/foldgutter.js',
      '/lib/codemirror-5.31.0/addon/fold/brace-fold.js',
      '/lib/codemirror-5.31.0/addon/fold/indent-fold.js',
      '/lib/codemirror-5.31.0/addon/fold/comment-fold.js',
      '/lib/codemirror-5.31.0/addon/edit/closebrackets.js',
      '/lib/codemirror-5.31.0/addon/edit/matchbrackets.js',
      '/lib/codemirror-5.31.0/mode/clike/clike.js',
      '/lib/codemirror-5.31.0/addon/display/autorefresh.js',
      '/lib/codemirror-5.31.0/addon/display/fullscreen.js',
      '/lib/codemirror-5.31.0/addon/merge/merge.js',


    ];


    loadjs(urls, {
      async: false,
      success: function() {
        window.loadCodemirror = true
        resolve();
      },
    });
  });
}

// 引入 codemirror
export function loadBeautify() {
  return new Promise((resolve, reject) => {
      // 远程加载js （按需引入）

    if(window.loadBeautify){
      resolve();
      return;
    }

    var urls = [
      // js
      '/lib/js-beautify/beautify-html.min.js',
    ];


    loadjs(urls, {
      async: false,
      success: function() {
        window.loadBeautify = true
        resolve();
      },
    });
  });
}


/*********************** 编辑器相关 ***********************/

export function createEditor(dom, params = {}) {
  var el = dom;
  var initValue = '';
  var myCodeMirror = CodeMirror.fromTextArea(el, {
    mode: 'text/x-sql', // 语言模式
    theme: 'material', // 主题
    keyMap: 'default', // 快键键风格
    lineNumbers: true, // 显示行号
    smartIndent: true, // 智能缩进
    indentUnit: 4, // 智能缩进单位为4个空格长度
    indentWithTabs: true, // 使用制表符进行智能缩进
    lineWrapping: true, // 是否换行
    // 在行槽中添加行号显示器、折叠器、语法检测器
    gutters: [
      'CodeMirror-linenumbers',
      'CodeMirror-foldgutter',
      'CodeMirror-lint-markers',
    ],
    autofocus: true, // 自动聚焦
    matchBrackets: true, // 匹配结束符号，比如"]、}"
    autoCloseBrackets: true, // 自动闭合符号
    styleActiveLine: true, // 显示选中行的样式
    extraKeys: { Ctrl: 'autocomplete' }, //自动提示配置
    ...params,
  });
  // 设置初始文本，这个选项也可以在fromTextArea中配置
  myCodeMirror.setOption('value', initValue);
  // 编辑器按键监听
  myCodeMirror.on('keypress', function () {
    // 没禁用状态显示智能提示
    if (!myCodeMirror.isReadOnly()) {
      // 显示智能提示
      // myCodeMirror.showHint(); // 注意，注释了CodeMirror库中show-hint.js第131行的代码（阻止了代码补全，同时提供智能提示）
    }
  });
  myCodeMirror.setSize('auto', '100%');
  return myCodeMirror;
}

// 包裹svg
export function svgHtml(svg) {
    return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
  <style>
    html{
        width: 100%;
        height: 100%;
    }
    body{
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        min-height: 100vh;
        margin: 0;
    }
    .box {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>

<body>
   <div class="box">
      ${svg}
   </div>
</body>
</html>`
}